package com.keda.controller;

import com.keda.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 1. 接收参数
        String username = request.getParameter("userName");
        String password = request.getParameter("password");
        String rePassWord = request.getParameter("rePassWord");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String dentityCode = request.getParameter("dentityCode");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");

        // 2.调用UserService注册
        UserService userService = new UserService();
        String message = userService.register(username, password, rePassWord,
                sex, birthday, dentityCode, email, mobile, address, request.getSession());

        // 3.返回结果
        if ("注册成功！".equals(message)) {
//            request.getRequestDispatcher("register-success.jsp").forward(request, response);
            response.sendRedirect("/index");
            return;
        }
        request.setAttribute("error", message);
        request.getRequestDispatcher("register.jsp").forward(request, response);

    }
}
